-- ==============================================
-- local variables
DECLARE	@cursor CURSOR
			, @o NVARCHAR(256)
			, @p XML, @d XML, @m XML
-- ==============================================


-- first, drop/create dpnd tables so they are empty


-- ============
/*<code_comment><![CDATA[begin cursor: for each routine]]></code_comment>*/
SET @cursor = CURSOR FOR
	SELECT SPECIFIC_SCHEMA + '.' + SPECIFIC_NAME
	FROM INFORMATION_SCHEMA.ROUTINES
	WHERE SPECIFIC_NAME NOT IN (
		'sp_upgraddiagrams'
		, 'sp_helpdiagrams'
		, 'sp_helpdiagramdefinition'
		, 'sp_creatediagram'
		, 'sp_renamediagram'
		, 'sp_alterdiagram'
		, 'sp_dropdiagram'
		, 'fn_diagramobjects'
	)

OPEN @cursor

FETCH NEXT FROM @cursor INTO @o

WHILE @@FETCH_STATUS <> -1
BEGIN
	-- ============
	/*<code_comment><![CDATA[get header and insert]]></code_comment>*/
	BEGIN TRY
		-- parse
		EXECUTE ssf.dpnd_parse_object_header @o, @p OUTPUT
		
		-- derive
		EXECUTE ssf.dpnd_derive_object_header @o, @d OUTPUT
		
		-- merge
		SET @m = ssf.dpnd_merge_parsed_with_derived_object_header(@p, @d)
		
		-- insert
		EXECUTE ssf.dpnd_insert_object_header @m
	END TRY
	BEGIN CATCH
		SELECT @o, ERROR_MESSAGE()
	END CATCH
	-- ============

	FETCH NEXT FROM @cursor INTO @o
END
/*<code_comment><![CDATA[end cursor: for each routine]]></code_comment>*/
-- ============
